<template>
	<div>
		<el-descriptions :title="`批次号:${nowData.batchNumber}`" :column="2">
			<el-descriptions-item label="采集人">
				{{ nowData.gathererName }}
			</el-descriptions-item>
			<el-descriptions-item label="采集内容">
				{{ nowData.gatherContent }} (已入库 {{ nowData.successSum }}, 采集成功率 {{ nowData.successPercent ? nowData.successPercent + '%' : '' }} )
			</el-descriptions-item>
			<el-descriptions-item label="采集时间">
				{{ nowData.createTime }}
			</el-descriptions-item>
			<el-descriptions-item label="采集人所属单位">
				{{ nowData.unitName }}
			</el-descriptions-item>
		</el-descriptions>

		<div class="step">
			<el-steps :active="activeStatus">
				<el-step title="数据校验" @click.native="handleLinkDetail(1)">
					<template v-slot:description>
						<span>完成时间: {{ nowData.verifyFinishTime }}</span>
					</template>
				</el-step>
				<el-step title="数据比对" @click.native="handleLinkDetail(2)">
					<template v-slot:description>
						<span>完成时间: {{ nowData.compareFinishTime }}</span>
					</template>
				</el-step>
				<el-step title="成功入库" @click.native="handleLinkDetail(3)">
					<template v-slot:description>
						<span>完成时间: {{ nowData.gatherFinishTime }}</span>
					</template>
				</el-step>
			</el-steps>
		</div>
		<LogList :key="reloadLog" v-if="nowData.tableName" :dataStatus="activeStatus" :versionNumber="nowData.versionNumber" :tableName="nowData.tableName" :batchNumber="batchNumber"></LogList>
		<div class="footer-btn tc">
			<el-button text type="primary" plain @click="handleDownload"> 下载文件 </el-button>
			<el-button v-if="activeStatus == 2 && nowData.gatherFinishTime && isAppeal != 1" text type="danger" plain @click="drawer = !drawer">
				提交申诉
			</el-button>
		</div>

		<el-drawer v-model="drawer" title="数据采集比对异常结果申诉" append-to-body>
			<el-descriptions :title="`申述数据批次号:${nowData.batchNumber}`" :column="2">
				<el-descriptions-item label="申诉人">
					{{ nowData.gathererName }}
				</el-descriptions-item>

				<el-descriptions-item label="申诉时间">
					{{ nowData.gatherFinishTime }}
				</el-descriptions-item>
				<el-descriptions-item label="申诉内容">
					{{ nowData.gatherContent }}
				</el-descriptions-item>
				<el-descriptions-item label="申诉人所属单位">
					{{ nowData.unitName }}
				</el-descriptions-item>
				<el-descriptions-item label="申诉类型"> 基础数据异常申诉 </el-descriptions-item>
			</el-descriptions>

			<AplidForm v-if="drawer" v-bind="ApprovelFormConfig" @submit="handleAppeal" v-model="formModel"> </AplidForm>
		</el-drawer>
	</div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import LogList from './LogList/index.vue'
import {ElMessage} from 'element-plus'
import { ApprovelFormConfig } from './config'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
const { downloadFile } = useOperate()

type Props = {
	batchNumber: string
	appealType?: number
	dataType: string | number
	isAppeal?: string | number
}
const props = withDefaults(defineProps<Props>(), {
	appealType: 0, //申诉类型(0基础数据异常,1业务数据异常)
})

const drawer = ref(false)
const activeStatus = ref(1)
const nowData: any = ref({})
const reloadLog = ref(0)
const formModel = ref({
	appealStaus: 0,
	dataType: props.dataType,
	appealType: props.appealType,
	appealContent: nowData.value.gatherContent,
	batchNumber: props.batchNumber,
})

const getInitData = () => {
	apiMod
		.view_dpGatherForm_form({
			batchNumber: props.batchNumber,
		})
		.then((res) => {
			nowData.value = res.data
			activeStatus.value = nowData.value.gather_status || 1
		})
}

const handleLinkDetail = (key) => {
	activeStatus.value = key
	reloadLog.value = Math.random()
}
// 提交申诉
const handleAppeal = (form, done) => {
	apiMod
		.view_dpExceptionAppeal_appeal({
			...form,
			tableName: nowData.value.tableName,
		})
		.then((res) => {
			drawer.value = false
			ElMessage.success('提交成功!')
			done()
		})
		.catch((res) => {
			done()
		})
}
// 下载文件
const handleDownload = () => {
	let query: any = {
		batchNumber: props.batchNumber,
	}
  if (activeStatus.value == 1) {
    query.dataStatusList = [1, 4, 5]
  }else if (activeStatus.value == 3) {
		query.dataStatusList = [3]
	} else {
		query.dataStatus = activeStatus.value
	}
	downloadFile('view_dpDetails_export', query)
}
onMounted(() => {
	getInitData()
})
</script>

<style scoped></style>
